package com.wonder.sms.smsplannet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wonder.admin.service.SysConfigService;
import com.wonder.admin.domain.Parameter;
import com.wonder.admin.service.ParameterService;
import com.wonder.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsPlannetMessageSendService {

    @Autowired
    SysConfigService sysConfigService;

    @Autowired
    ParameterService parameterService;

    public String sendSms(String phone, String code){

        String url = sysConfigService.getCacheKey("smsplannet_url"); //"http://api.wftqm.com";
        String appKey = sysConfigService.getCacheKey("smsplannet_appKey");//"n4rJhXBX";
        String secretkey = sysConfigService.getCacheKey("smsplannet_secretkey"); //"uaLEXy5b";
        String content = parameterService.getKey("system.sms.smsplannet.body"); //"THR OTP code : {code}";
        content = content.replace("{code}",code);

        String res = HttpUtils.sendPost(url+"/api/sms/mtsend","appkey=" + appKey +
                "&secretkey=" + secretkey +
                "&phone="+phone+"&content="+content);

        JSONObject resObj = JSONObject.parseObject(res);

        if(resObj.get("code").equals("0")) return "ok";

        return resObj.get("result").toString();
    }
}
